package com.stlm2.dems.service;

import com.stlm2.core.common.page.PageView;
import com.stlm2.dems.dao.DdmGoodPropertyRelationDao;
import com.stlm2.dems.dao.UserDao;
import com.stlm2.dems.entity.DdmGoodPropertyRelation;
import com.stlm2.dems.entity.User;
import com.stlm2.util.Memcached;
import com.stlm2.util.codecontent.CodeContent;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.math.NumberUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;

import com.stlm2.dems.entity.DdmGoodProperty;
import com.stlm2.dems.dao.DdmGoodPropertyDao;
import com.stlm2.core.base.BaseDao;
import com.stlm2.core.base.impl.BaseServiceImpl;
import org.springframework.ui.ModelMap;

import java.util.ArrayList;
import java.util.List;

@Service("ddmGoodPropertyService")
@Transactional(rollbackForClassName={"java.lang.Exception"}, propagation = Propagation.REQUIRED)
public class DdmGoodPropertyService extends BaseServiceImpl<DdmGoodProperty> {

	@Resource
	private DdmGoodPropertyDao ddmGoodPropertyDao;
	@Resource
	private UserDao userDao;
	@Resource
	private Memcached memcached;
	@Resource
	private DdmGoodPropertyRelationDao ddmGoodPropertyRelationDao;

	@Override
	protected BaseDao <DdmGoodProperty> getDao() {
		return ddmGoodPropertyDao;
	}

	/*
*查询所有的属性名称
* zlq
 */
	public String getAllGoodPropertyName(ModelMap model) {
		DdmGoodProperty ddmGoodProperty = new DdmGoodProperty();
		List<DdmGoodProperty> ddmGoodPropertyList = ddmGoodPropertyDao.queryPageView(-1, -1, ddmGoodProperty).getRecords();
		return CodeContent.getModelByCode(1, "", convertList(ddmGoodPropertyList));
	}

	public List<ModelMap> convertList(List<DdmGoodProperty> ddmGoodPropertyList){
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (ddmGoodPropertyList != null && ddmGoodPropertyList.size() > 0) {
			for (DdmGoodProperty ddmGoodProperty  : ddmGoodPropertyList) {
				ModelMap map = buildModelList(ddmGoodProperty);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList(DdmGoodProperty ddmGoodProperty) {
		ModelMap model = new ModelMap();
		model.put("id",ddmGoodProperty.getId());
		model.put("name",ddmGoodProperty.getName());
		return  model;
	}

	/*
*CRM查询商品基础属性列表
* zlq
* token:
* page:页码
 */
	public String getList(ModelMap model, String token,String page){
		PageView pageview = new PageView();
		int pageNo = 1;
		int pageSize = 20;
		if (NumberUtils.isDigits(page) && !"0".equals(page.trim())) {
			pageNo = NumberUtils.toInt(page);
		}
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		DdmGoodProperty ddmGoodProperty=new DdmGoodProperty();
		pageview=ddmGoodPropertyDao.queryPageView(pageNo, pageSize,ddmGoodProperty);
		model.put("code",1);
		model.put("msg","success");
		if (pageview != null){
			model.put("datas",convertCrmList(pageview.getRecords()));
			model.put("recordsTotal", pageview.getRowCount());
			model.put("totalPage", pageview.getPageCount());
			model.put("pageNow", pageview.getPageNow());
		}
		return new JSONObject(model).toString();

	}
	public List<ModelMap> convertCrmList(List<DdmGoodProperty> ddmGoodPropertyList) {
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (ddmGoodPropertyList != null && ddmGoodPropertyList.size() > 0) {
			for (DdmGoodProperty ddmGoodProperty : ddmGoodPropertyList) {
				ModelMap map = buildModelCrmList(ddmGoodProperty);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelCrmList(DdmGoodProperty ddmGoodProperty) {
		ModelMap model = new ModelMap();
		model.put("id",ddmGoodProperty.getId());
		model.put("name",ddmGoodProperty.getName());
		model.put("label",ddmGoodProperty.getLabel());
		return  model;
	}

	public  String add(ModelMap model, String token,String name,String label){
		if (StringUtils.isEmpty(token)) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		User loginUser = userDao.getByToken(token);
		if (loginUser == null) {
			return CodeContent.getModelByCode(-1, "", model);
		}
		if(StringUtils.isEmpty(name)){
			return CodeContent.getModelByCode(0, "属性名称不能为空", model);
		}
		if(StringUtils.isEmpty(label)){
			return CodeContent.getModelByCode(0, "属性标签名不能为空", model);
		}
		//防止重复提交
		Object o = memcached.get(loginUser.getId()+"ADDDdmGoodProperty");
		if(o!=null){
			return CodeContent.getModelByCode(0, "您操作频繁，请在5秒后重试", model);
		}else {
			memcached.set(loginUser.getId()+"ADDDdmGoodProperty",5,"1");
		}
		DdmGoodProperty ddmGoodProperty=ddmGoodPropertyDao.getByLabel(label.trim());
		if(ddmGoodProperty!=null){
			return CodeContent.getModelByCode(0, "属性标签名已存在", model);
		}else {
			ddmGoodProperty=new DdmGoodProperty();
		}
		ddmGoodProperty.setName(name.trim());
		ddmGoodProperty.setLabel(label.trim());
		ddmGoodPropertyDao.add(ddmGoodProperty);
		return CodeContent.getModelByCode(1, "", model);
	}


   //获取商品
	public String getAllGoodPropertyName2(ModelMap model,String goodsId) {
		DdmGoodProperty ddmGoodProperty = new DdmGoodProperty();
		List<DdmGoodProperty> ddmGoodPropertyList = ddmGoodPropertyDao.queryPageView(-1, -1, ddmGoodProperty).getRecords();
		return CodeContent.getModelByCode(1, "", convertList2(ddmGoodPropertyList,goodsId));
	}

	public List<ModelMap> convertList2(List<DdmGoodProperty> ddmGoodPropertyList,String goodsId){
		List<ModelMap> list = new ArrayList<ModelMap>();
		if (ddmGoodPropertyList != null && ddmGoodPropertyList.size() > 0) {
			for (DdmGoodProperty ddmGoodProperty  : ddmGoodPropertyList) {
				ModelMap map = buildModelList2(ddmGoodProperty,goodsId);
				list.add(map);
			}
		}
		return list;
	}
	public ModelMap buildModelList2(DdmGoodProperty ddmGoodProperty,String goodsId) {
		ModelMap model = new ModelMap();
		model.put("id",ddmGoodProperty.getId());
		model.put("name",ddmGoodProperty.getName());
		Boolean ifShow=false;
		if(NumberUtils.isDigits(goodsId)){
			DdmGoodPropertyRelation goodPropertyRelation=ddmGoodPropertyRelationDao.getRelation(ddmGoodProperty.getId(),NumberUtils.toInt(goodsId));
			if(goodPropertyRelation!=null){
				ifShow=true;
			}
		}
		model.put("ifShow",ifShow);
		return  model;
	}
}